package org.duang.plugins;

import java.util.HashMap;
import java.util.Map;

import org.duang.config.Config;
import org.duang.db.Connection;
import org.duang.db.mongo.Mongo;
import org.duang.kit.ToolsKit;

public class MongodbPlugin implements IPlugin {

	public void start() throws Exception{
		Map<String, String> optionMap = new HashMap<String,String>();
		optionMap.put(Connection.PORT_FIELD, Config.getMongoDBPort());
		optionMap.put(Connection.HOST_FIELD, Config.getMongoDBHost());
		optionMap.put(Connection.DATABASE_FIELD, Config.getMongoDBName());
		if(ToolsKit.isNotEmpty(Config.getMongoDBUserName())){
			optionMap.put(Connection.USERNAME_FIELD, Config.getMongoDBUserName());	
		}
		if(ToolsKit.isNotEmpty(Config.getMongoDBPassword())){
			optionMap.put(Connection.PASSWORD_FIELD, Config.getMongoDBPassword());	
		}
		optionMap.put(Connection.REPLICASET_FIELD, Config.getMongoDBReplicaset());
		Mongo.getInstance().connect(optionMap);
	}

	public void stop() throws Exception{
		Mongo.getInstance().close();
	}

}
